﻿var opttype = 1; //全局 编辑新增控制,默认1：新增，2，修改
jQuery(function () {
    InitButton();
    //提交
    function submitPage(dataModel, opttype) {
       
        var url = document.location.href;
        var queryStringlist = url.split('&');
        ///如果校验失败,则不执行保存方法
        if (ProcessBeforeSave(modelName, opttype)) {
            Save(modelName, dataModel, opttype);
        }
    }
    //保存
    jQuery('#btnsave').submit(function () {
      
        ///如果是处于查看状态,则关闭该窗口
        if (wmsCommon.getQueryValue("opt") == "3") document.close();
        if (submitValidatePage()) {
            debugger;
            var dataModel = jQuery('#form1').formSerialize();
            opttype = wmsCommon.getQueryValue("opt");
            //;
            if (jQuery('#btnsave').attr("method") != undefined) {
                var expr = jQuery('#btnsave').attr("method");
                expr = expr.replace(/{data}/g, dataModel);
                expr = expr.replace(/{opttype}/g, opttype);
                wmsCommon.execCommand(expr);
            }
            else {
                submitPage(dataModel);
            }
        }
        return false;
    });
    ///重置
    jQuery('#btnreset').submit(function () {
        //;
        jQuery('#form1').clearForm();
        return false;
    })
    ///初始化按钮,如果是删除按钮,则绑定删除事件,如果是新增按钮,绑定新增事件
    function InitButton() {
        var buttons = $("input:submit");
        for (var i = 0; i < buttons.length; i++) {
            var btnid = buttons[i].id;
            if (btnid.indexOf('$') > -1) {
                jQuery(buttons[i]).submit(function () {
                    // ;
                    var btn = jQuery(this);
                    var tableid = btn[0].id.split('$')[0];
                    var type = btn[0].id.split('$')[1];
                    if (type == "del") {
                        DelRow(tableid);
                    }
                    else if (type == "add" || type == "new") {
                        AddRow(tableid);
                    }
                    return false;
                });
            }
        }
    }

    //岗位功能授权--ydl
    function RoleAuthorization() {

    }

    ///新增明细
    function AddRow(tableid) {
        primarykey = $.trim(wmsCommon.getQueryValue("id"));
        var expr = "sys(#DataModelManager.GetwmsCommon.newRow(" + modelName + "," + tableid + "," + GetColumns(tableid) + "," + primarykey + "))";
        var data = wmsCommon.execCommand(expr);
        jQuery('#' + tableid + '').append(data);

    }



});

///删除明细
function DelRow(tableid) {
    var inputList = jQuery('#' + tableid + ' tr td input')
    for (var i = 0; i < inputList.length; i++) {
        if (inputList[i].type == "checkbox") {
            if (inputList[i].checked) {
                jQuery(inputList[i].parentNode.parentNode).remove();
            }
        }
    }
}
///获取显示列的列表
function GetColumns(tableid) {
    var tr = $('#' + tableid + ' tr:first-child');
    var columns = "";
    if (tr.length != 0) {
        for (var i = 0; i < tr[0].childNodes.length; i++) {
            if (tr[0].childNodes[i].childNodes.length == 0) continue;
            var id = tr[0].childNodes[i].childNodes[0].id;
            columns += id.slice(id.indexOf('_') + 1, id.length) + "&";
        }
    }
    return columns;
}
///保存后执行函数,后代可重写该方法
function ProcessAfterSave(url) {
    // wmsCommon.alert(url);
}
///保存前执行函数,后代可重写该方法
function ProcessBeforeSave(modelName, opttype) {
    return true;
}
///重新获取页面的Model
function GetModel() {
    return jQuery('#form1').formSerialize();
}

//function ShowDialog(message) {
//    $("#dialog-confirm").text(message);
//    $("#dialog-confirm").dialog({
//        resizable: false,
//        height: 140,
//        modal: true,
//        buttons: {
//            "关闭": function () {
//                $(this).dialog("close");
//            }
//        }
//    });
//}

///保存
function Save(modelName, dataModel, opttype) {
  
    var templateName = "";
    var queryStringlist = document.location.href.split('&');
    for (var i = 0; i < queryStringlist.length; i++) {
        if (queryStringlist[i].indexOf("opt") > -1) {
            opttype = queryStringlist[i].split('=')[1];
        }
        else if (queryStringlist[i].indexOf("temp") > -1) {
            templateName = queryStringlist[i].split('=')[1];
        }
    }
    var expr = "sys(#DataModelManager.SaveModel(" + modelName + "," + dataModel + "," + opttype + "))";
    var data = wmsCommon.execCommand(expr);
    if (data) {
        wmsCommon.showDialog('保存成功');
        if (opttype == 1) {
            {
                document.location.href = "Model.aspx?model=" + modelName + "&temp=" + templateName + "&id=" + data.d + "&opt=2";
                opttype = '2';
            }
        }
        else {

        }
        ProcessAfterSave(document.location.href);
    }

};